/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "../colibri"
import "../colibri/gradients"
import "styles"

Rectangle {
    id: window

    property CLStyle style: StyleHomeControlSystem {}
    property int playTime: 3*60*1000
    property bool musicOn: true
    property bool moviesOn: false
    property ListModel movieList: MovieList {}
    property ListModel musicList: MusicList {}
    property ListModel temp: ListModel {}
    property Image playImage: Image { source: "images/play_100x100.png"; width: window.width*0.04; height: window.height*0.06; smooth: true }
    property Image pauseImage: Image { source: "images/pause_100x100.png"; width: window.width*0.04; height: window.height*0.06; smooth: true }
    property Image stopImage: Image { source: "images/stop_100x100.png"; width: window.width*0.04; height: window.height*0.06; smooth: true }
    property Image prevImage: Image { source: "images/prev_100x120.png"; width: window.width*0.04; height: window.height*0.06; smooth: true }
    property Image nextImage: Image { source: "images/next_100x120.png"; width: window.width*0.04; height: window.height*0.06; smooth: true }

    signal closeWindow()


    /**
     * Sets random values to equalizer-histogram.
     */
    function equalizerRandomizer() {
        var array = new Array()
        for(var i=0; i<8; i++) {
           array[array.length] = new Array(Math.random()*100,"1")
        }
        equalizer.setValues(array);
    }


    /**
     * Sets zero values to equalizer-histogram.
     */
    function equalizerZeroer() {
         var array = new Array()
        for(var i=0; i<8; i++) {
           array[array.length] = new Array(0,"1")
        }
        equalizer.setValues(array);
    }

    /**
     * Function which is called when user press play-button.
     */
    function play() {
        timerEqualizer.running = !timerEqualizer.running;
        timerDuration.running = !timerDuration.running;
        if(timerEqualizer.running) {
            playCLButton.backgroundImage = pauseImage;
            playCLButton.backgroundImageWhenHovered = pauseImage;
            playCLButton.backgroundImageWhenPressed = pauseImage;
        } else {
            playCLButton.backgroundImage = playImage;
            playCLButton.backgroundImageWhenHovered = playImage;
            playCLButton.backgroundImageWhenPressed = playImage;
        }
    }

    /**
     * Function which is called when user press stop-button
     */
    function stop() {
        duration.setValue(0);
        timerEqualizer.running = false
        timerDuration.running = false

        playCLButton.backgroundImage = playImage;
        playCLButton.backgroundImageWhenHovered = playImage;
        playCLButton.backgroundImageWhenPressed = playImage;

        equalizerZeroer()
    }

    /**
     * Function is called When cover is changed in coverflow. This
     * Function changes information text about track (artist name, track name, album name)
     * @param index New selected index in coverflow
     * @return nothing
     */
    function coverChangedFunction(index) {
        duration.setValue(0);
        if(musicOn) {
            artistText.text = musicList.get(index).artist
            trackText.text = musicList.get(index).track
            albumText.text = musicList.get(index).album
            duration.value = 0
            duration.maximum = musicList.get(index).length*1000
        } else {
            artistText.text = movieList.get(index).movie
            trackText.text = movieList.get(index).director
            albumText.text = movieList.get(index).year
            duration.value = 0
            duration.maximum = movieList.get(index).length*1000
        }
    }

    /**
     * Rolls coverflow to right.
     */
    function next() {
        covers.next();
        duration.setValue(0);
    }

    /**
     * Rolls coverflow to left.
     */
    function previous() {
        covers.previous();
        duration.setValue(0);
    }


    /**
     * Functions formats time in milliseconds to minutes : seconds format.
     * @param durationMilliSeconds Time in milliseconds.
     * @return time in "minutes : seconds" -format
     * @example
     * formatDurationString(60005) -> 1:05
     */
    function formatDurationString(durationMilliSeconds) {
        var minutes = parseInt(durationMilliSeconds / 1000 / 60);
        var seconds =  parseInt(durationMilliSeconds / 1000) % 60;
        if(seconds < 10) seconds = "0" + seconds;
        return minutes + ":" + seconds;
    }

    Component.onCompleted: {
        var array = new Array(new Array(0,"1"),new Array(0,"1"),new Array(0,"1"),new Array(0,"1"),new Array(0,"1"),new Array(0,"1"), new Array(0,"1"),new Array(0,"1"))
        equalizer.setValues(array);
        covers.imageSize = window.height*0.32
        covers.height = covers.imageSize
    }

    onWidthChanged: {
        covers.imageSize = window.height*0.32
        covers.height = covers.imageSize
    }

    onHeightChanged: {
        covers.imageSize = window.height*0.32
        covers.height = covers.imageSize
    }

    width: 640
    height: 424
    color: "transparent"

    Timer {
        id: timerEqualizer
        interval: 75; running: false; repeat: true
        onTriggered: {
            equalizerRandomizer();
        }
    }

    Timer {
        id: timerDuration
        interval: 1000; running: false; repeat: true
        onTriggered: {
           if(duration.value >= duration.maximum) next();
           duration.setValue(duration.value + interval);
        }
    }

    Rectangle {
        width:window.width*0.955
        height:window.height*0.924
        color:style.selectionColor
        anchors.centerIn:parent
        radius:7
    }

    Image {
        source: "images/control_bg_trans_420x275_png.png";
        anchors.fill: parent;
        smooth: true
        opacity:0.7
    }

    RoomExitButton {
        windowWidth: window.width
        windowHeight: window.height
        style: window.style
        onClicked: { console.log("2"); window.closeWindow(); }
    }

    CLHistogram {
        id: equalizer
        style: window.style
        colorWhenDefault: window.style.textColor
        gradientDefaultOn: false
        maxValue: 100
        anchors.horizontalCenter: parent.horizontalCenter
        width: window.width*0.9
        height: window.height*0.9
        opacity: 0.3
        y: window.height*0.06
    }


    Column {
        x: 0.05*window.height
        y: 0.05*window.height
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: window.height*0.02

        Row {
            spacing: window.width*0.04
            Text {
                id: header
                text: "Music"
                font.pixelSize: 1 + Math.round( 23 * (window.height / 424) )
                color: window.style.textColor
                font.bold: true
            }
            /*CLButton { id: musicCLButton; style: window.style;  text: "Music"; onClicked: selectMusic(); fontSize: window.height*0.03; selected: true; smooth: true }
            CLButton { id: movieCLButton; style: window.style; text: "Movies"; onClicked: selectMovie(); fontSize: window.height*0.03; smooth: true } */
        }

        Column {
            z:1
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: window.height*0.01
            Text { id: artistText; font.pointSize: 0.001+window.height*0.04; anchors.horizontalCenter : parent.horizontalCenter; color: window.style.textColor }
            CLCarousel { id: covers; onCoverChanged: coverChangedFunction(index); coverList: musicList; imageAmount: 5; imageSize: window.height*0.5; width: window.width*0.9 }
            Text { id: trackText; font.pointSize: 0.001+window.height*0.04; anchors.horizontalCenter : parent.horizontalCenter; color: window.style.textColor }
            Text { id: albumText; font.pointSize: 0.001+window.height*0.03; anchors.horizontalCenter : parent.horizontalCenter; color: window.style.textColor }
        }

        Column {
            spacing: window.height*0.05

            Row {
                anchors.horizontalCenter: parent.horizontalCenter;
                spacing: window.width*0.04
                Text { text: formatDurationString(duration.value); font.pointSize: 0.001+window.height*0.04; color: window.style.textColor; anchors.verticalCenter : parent.verticalCenter }
                CLSlider { id: duration; style: window.style; margin: window.height*-0.04; width: window.width*0.6; height: window.height*0.03; sliderSize: window.width*0.02; minimum: 0; maximum: playTime; anchors.verticalCenter: parent.verticalCenter }
                Text { text: formatDurationString(duration.maximum); font.pointSize: 0.001+window.height*0.04; color: window.style.textColor; anchors.verticalCenter : parent.verticalCenter }
            }
            Row {
                spacing: 5
                anchors.horizontalCenter : parent.horizontalCenter
                CLButton { id: playCLButton; text: ""; style: window.style; onClicked: play(); width: window.width*0.12; height: window.height*0.12; backgroundImage: playImage; backgroundImageWhenHovered: playImage; backgroundImageWhenPressed: playImage; smooth: true }
                CLButton { id: stopCLButton; text: ""; onClicked: stop(); style: window.style; width: window.width*0.12; height: window.height*0.12;backgroundImage: stopImage; backgroundImageWhenHovered: stopImage; backgroundImageWhenPressed: stopImage; smooth: true }
                CLButton { id: previousCLButton; onClicked: previous(); text: ""; style: window.style; width: window.width*0.12; height: window.height*0.12; backgroundImage: prevImage; backgroundImageWhenHovered: prevImage; backgroundImageWhenPressed: prevImage; smooth: true }
                CLButton { id: nextCLButton; onClicked: next(); text: ""; style: window.style; width: window.width*0.12; height: window.height*0.12; backgroundImage: nextImage; backgroundImageWhenHovered: nextImage; backgroundImageWhenPressed: nextImage; smooth: true }
            }
        }
     }
 }
